
var searchParam={
    searchkw:""
}
layui.use(["table","form"], function () {
    var table = layui.table,
    form=layui.form;
    table.render({
        elem: "#table",
        url: BASE_PATH + "/app/icon/getAll",
        page: true,
        id:"currentTableId",
        toolbar: '#toolbarDemo',
        height: "full-120",
        autoSort: false,
        cols: [
            [
                {
                    align: "center",
                    minWidth: 80,
                    height:80,
                    field: "iconPath",
                    title: "图标",
                    templet: function (d) {
                        var str = '<img src="' + BASE_PATH + d.iconPath+'" style="height:40px">'
                        return str;
                    }
                },
                {
                    align: "center",
                    field: "name",
                    minWidth: 150,
                    title: "图标名"
                }, {
                    align: "center",
                    field: "sort",
                    minWidth: 80,
                    title: "排序号"
                },{
                    align: "center",
                    field: "isDisplay",
                    minWidth: 100,
                    title: "显示",
                    templet:function(d){
                        var disp="";
                        if(d.isDisplay==0){
                            disp='<a class="layui-btn layui-btn-primary layui-btn-xs " lay-event="xs"><i class="layui-icon">&#xe6b2;</i>不显示</a>';
                        }else if(d.isDisplay==1){
                            disp='<a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="xs"><i class="layui-icon">&#xe6b2;</i>显示</a>';
                        }
                        return disp;
                    }
                },
                {
                    align: "center",
                    field: "url",
                    minWidth: 240,
                    title: "地址"
                },
                {
                    align: "center",
                    minWidth: 240,
                    field: "id",
                    title: "操作",
                    templet: function (d) {
                        var strf = '<a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="zd"><i class="layui-icon">&#xe604;</i>置顶</a>';
                        strf+='<a class="layui-btn layui-btn-normal layui-btn-xs data-count-edit" lay-event="edit"><i class="layui-icon">&#xe63c;</i>编辑</a>'
                        var str =
                            '<a  class="layui-btn layui-btn-danger layui-btn-xs"  lay-event="delete"><i class="layui-icon">&#xe640;</i>删除</a>';
                        return strf + str;
                    },
                },
            ],
        ],
        //主要利用回调渲染
        done: function (res, curr, count) {
            //然后把值写上去td[data-field="itemNo" 定义在哪个列后显示
        },
    });

    /**
     * toolbar事件监听
     */
    table.on('toolbar(myt)', function (obj) {
        if (obj.event === 'add') {   // 监听添加操作
            add();
        }
    });
    table.on('tool(myt)', function (obj) {
        var data = obj.data;
        if (obj.event === 'delete') {
            deleteItem(data.id);
            return false;
        } else if (obj.event === 'zd') {
            zd(data.id);
            return false;
        } else if (obj.event === 'xs') {
            xs(data);
            return false;
        } else if (obj.event === 'edit') {
            edit(data.id);
            return false;
        }
    });
    // 监听搜索操作
    form.on('submit(data-search-btn)', function (data) {
        searchParam.searchkw = data.field.searchkw
        //执行搜索重载
        table.reload('currentTableId', {
            page: {
                curr: 1
            }
            , where: searchParam
        });
        return false;
    });
    //编辑
    function edit(id){
        window.location.href="release_upload.html?id="+id;
    }
    //工具栏增加
    function add() {
        window.location.href="release_upload.html";
    }
    //列置顶
    function zd(id) {
        $.ajax({
            type: "post",
            url: BASE_PATH + "/app/icon/zd",
            dataType: "json",
            data: { id: id},
            success: function (data) {
                if (data.code == 1) {
                    table.reload("currentTableId", {
                        where:searchParam
                    });
                } else {
                    layer.msg(data.msg);
                }
            },
        });
    }
    //删除
    function deleteItem(id) {
        layer.confirm("确认要删除操作?", {
            icon: 3,
            title: "提示",
            btn: ["确认", "取消"],
            btn1: function (index) {
                $.ajax({
                    type: "post",
                    url: BASE_PATH + "/app/icon/delete",
                    dataType: "json",
                    data: { id: id },
                    success: function (data) {
                        if (data.code == 1) {
                            table.reload("currentTableId", {
                                where:searchParam
                            });
                            layer.close(index);
                        } else {
                            layer.msg(data.msg);
                        }
                    },
                });
            },
            btn2: function (index) {
                layer.close(index);
            },
        });
    }
    //显示
    function xs(data){
        var odisplay=data.isDisplay==0?1:0;
        $.ajax({
            type: "post",
            url: BASE_PATH + "/app/icon/updatedisplay",
            dataType: "json",
            data: { id: data.id,isDisplay:odisplay },
            success: function (data) {
                if (data.code == 1) {
                    table.reload("currentTableId", {
                        where:searchParam
                    });
                } else {
                    layer.msg(data.msg);
                }
            },
        });
    }
});
